package com.lagou.webwork.service;

import com.lagou.webwork.dao.StudentDao;
import com.lagou.webwork.factory.StudentFactory;
import com.lagou.webwork.model.ListResult;
import com.lagou.webwork.model.Result;
import com.lagou.webwork.model.Student;

import javax.swing.text.Keymap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sjp
 * @date 2021/5/27 14:02
 * @description
 */
public class StudentService {
    private StudentDao _studentDao;

    public StudentService() {
        _studentDao = StudentFactory.getStudentDao();
    }

    /**
     * 获取学生信息列表
     *
     * @return
     */
    public ListResult<Student> getStudentList(List<Map<String, Object>> paramsMap, int page, int number) {
        int startPosition = (page - 1) * number;
        return _studentDao.getStudentList(paramsMap, startPosition, number);
    }

    /**
     * 根据Id获取学生对象
     *
     * @param id
     * @return
     */
    public ListResult<Student> getStudentModelById(int id) {
        return _studentDao.getStudentModelById(id);
    }

    /**
     * 根据学号获取学生信息
     *
     * @param sid
     * @return
     */
    public ListResult<Student> getStudentModelBySid(String sid) {
        return _studentDao.getStudentModelBySid(sid);
    }

    /**
     * 添加学生信息
     *
     * @param addStudent
     * @return
     */
    public Result<Student> addStudent(Student addStudent) {
        if (checkSidRepeat(addStudent)) {
            return new Result<>("200", new ArrayList<>(), 0, "学号重复，请重新输入！");
        }

        ListResult<Student> listResult = _studentDao.addStudent(addStudent);
        Result<Student> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (listResult.getTotalCount() > 0) {
            result.setDataList(listResult.getDataList());
            result.setTotalCount(1);
            result.setMsg("添加成功!");
        } else {
            result.setMsg("添加失败，请刷新后再试。");
        }

        return result;
    }

    /**
     * 更新学生信息
     *
     * @param student
     * @return
     */
    public Result<Student> updateStudent(Student student) {
        if (checkSidRepeat(student)) {
            return new Result<>("200", new ArrayList<>(), 0, "学号重复，请重新输入！");
        }


        ListResult<Student> listResult = _studentDao.updateStudent(student);
        Result<Student> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (listResult.getTotalCount() > 0) {
            result.setDataList(listResult.getDataList());
            result.setTotalCount(1);
            result.setMsg("更新成功!");
        } else {
            result.setMsg("更新失败，请刷新后再试。");
        }

        return result;
    }


    /**
     * 批量删除学生信息
     *
     * @param idList
     * @return
     */
    public Result<Integer> deleteStudentList(List<Integer> idList) {
        ListResult<Integer> listResult = _studentDao.deleteStudentList(idList);
        Result<Integer> result = new Result<>("200", new ArrayList<>(), 0, "");
        if (listResult.getTotalCount() > 0) {
            result.setDataList(listResult.getDataList());
            result.setTotalCount(1);
            result.setMsg("删除成功!");
        } else {
            result.setMsg("删除失败，请刷新后再试。");
        }

        return result;
    }

    /**
     * 判断学号是否重复
     *
     * @param student
     * @return
     */
    public boolean checkSidRepeat(Student student) {
        return _studentDao.checkSidRepeat(student);
    }
}
